angular.module('service', [])
    .controller('serviceCtrl', ['$scope', 'httpService', 'upload', '$state', 'alertmsg', '$interval', '$localStorage', function($scope, httpService, upload, $state, alertmsg, $interval, $localStorage) {
        $scope.user = {
            role: 'service'
        };

        $scope.bool = false;
        $scope.isheadimg = false;
        var firstsubmit = true;

        if(!isweixin) {
            var data_save = $localStorage.data_save;
            if(data_save && typeof data_save == 'object' && data_save.data_type == 'service') {
                $scope.user = $localStorage.data_save;
            }
        }
            

        $scope.$watch('user', function(a, b) {
            if(isMobile) {
                a.data_type = 'service';
                if(!isweixin) { $localStorage.data_save = a; }
            }
        });

        // 提交服务商注册
        $scope.isserviceform2 = false;
        $scope.isargee = true;
        $scope.submitService = function(bool, user) {
            var file = document.querySelector('#headimgservice').files[0];
            if (file) {
                if (firstsubmit) {
                    if (!bool) {
                        firstsubmit = false;
                        httpService.query('web/sign_service', upload.format(file, user, true), 'post', true).then(function(res) {
                            firstsubmit = true;
                            alertmsg(res.msg);
                            if (res.code == 200) {
                                $localStorage.data_save = '';
                                $state.reload();
                            }
                        }, function() {
                            firstsubmit = true;
                        });
                    } else {
                        $scope.isserviceform2 = true;
                    }

                } else {
                    alertmsg('请不要多次提交！');
                }

            } else {
                $scope.isheadimg = true;
                if (bool) {
                    $scope.isserviceform2 = true;
                }
            }

        };

        $(function() {
            $('#headimgservice').change(function(e) {
                $scope.isheadimg = false;
                $scope.$apply();
                var file = e.target.files || e.dataTransfer.files;
                var fReader = new FileReader();
                fReader.readAsDataURL(file[0]);
                fReader.onloadend = function(event) {
                    var img = document.getElementById("previewservice");
                    img.src = event.target.result;
                    img.setAttribute('data-action', 'zoom');
                    $(img).css("display", "block");
                }
            });
        });

        $scope.isserviceform1 = false;
        $scope.next = function(bool) {
            console.log('bool', bool)
            if (!bool) {
                $scope.bool = true;
            } else {
                $scope.isserviceform1 = true;
            }
        };

        // 发送手机验证码
        var regx = /^1[0-9]{10}$/;
        $scope.count_down_time = interval;
        $scope.isDisplayTime = false;
        $scope.sendCode = function(tel) {
            console.log('tel', tel)
            if (regx.test(tel)) {
                httpService.query('web/send_code', { tel: tel }, 'post').then(function(res) {
                    if (res.code == 200) {
                        countDown();
                    } else {
                        alertmsg(res.msg);
                    }
                });
            } else {
                alertmsg('请正确输入手机号码！');
            }
        };

        // 倒计时方法
        function countDown() {
            var timer;
            // 显示倒计时时间
            $scope.isDisplayTime = true;
            timer = $interval(function() {
                $scope.count_down_time -= 1;
                // 当倒计时结束时，结束显示正常
                if ($scope.count_down_time <= 0) {
                    // 结束倒计时器
                    $interval.cancel(timer);
                    // 将倒计时重新附值
                    $scope.count_down_time = interval;
                    // 设置为可提交状态
                    $scope.isabled = false;
                    // 显示为获取验证码
                    $scope.isDisplayTime = false;
                }
            }, 1000);
        };

    }]);
